import type { App } from "vue";
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";

export const Layout = () => import("@/layout/index.vue");

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
      },
    ],
  },

  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: { hidden: true },
  },

  {
    path: "/",
    name: "/",
    component: Layout,
    redirect: "/store",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        // 用于 keep-alive 功能，需要与 SFC 中自动推导或显式声明的组件名称一致
        // 参考文档: https://cn.vuejs.org/guide/built-ins/keep-alive.html#include-exclude
        name: "Dashboard",
        meta: {
          title: "dashboard",
          icon: "homepage",
          hidden: false,
          affix: true,
          keepAlive: true,
        },
      },
      {
        path: "401",
        component: () => import("@/views/error-page/401.vue"),
        meta: { hidden: true },
      },
      {
        path: "404",
        component: () => import("@/views/error-page/404.vue"),
        meta: { hidden: true },
      },
    ],
  },
  {
    path: "/store",
    component: Layout,
    redirect: "/store/index",
    name: "Store",
    meta: { title: "商家", icon: "el-icon-shop" },
    children: [
      {
        path: "index",
        component: () => import("@/views/store/index.vue"),
        name: "StoreIndex",
        meta: { title: "商家管理" },
      },
      {
        path: "new",
        component: () => import("@/views/store/new.vue"),
        name: "StoreNew",
        meta: { title: "新增商家" },
      },
      {
        path: "setting",
        component: () => import("@/views/store/setting.vue"),
        name: "StoreSetting",
        meta: { title: "商家聚合支付设置", hidden: true },
      },
      {
        path: "category",
        component: () => import("@/views/store/category.vue"),
        name: "StoreCategory",
        meta: { title: "商家分类" },
      },
      // {
      //   path: "demo",
      //   component: () => import("@/views/demo/upload.vue"),
      //   name: "demo",
      //   meta: { title: "商家分类" },
      // },
    ],
  },
  {
    path: "/payment",
    component: Layout,
    redirect: "/payment/index",
    name: "Payment",
    meta: { title: "聚合支付", icon: "el-icon-sell" },
    children: [
      {
        path: "store",
        component: () => import("@/views/payment/store.vue"),
        name: "PaymentStore",
        meta: { title: "主商家管理" },
      },
      {
        path: "store/sub",
        component: () => import("@/views/payment/store-sub.vue"),
        name: "PaymentStoreSub",
        meta: { title: "子商家管理" },
      },
      {
        path: "store/reivcer",
        component: () => import("@/views/payment/store-reivcer.vue"),
        name: "PaymentStoreReivcer",
        meta: { title: "绑定分账方", hidden: true },
      },
      {
        path: "store/reivcer/new",
        component: () => import("@/views/payment/store-reivcer-new.vue"),
        name: "PaymentStoreReivcerNew",
        meta: { title: "绑定分账方", hidden: true },
      },
      {
        path: "order",
        component: () => import("@/views/payment/order.vue"),
        name: "PaymentOrder",
        meta: { title: "交易订单" },
      },
      {
        path: "order/detail",
        component: () => import("@/views/payment/order-detail.vue"),
        name: "PaymentOrderDetail",
        meta: { title: "订单详情" },
      },
      {
        path: "split",
        component: () => import("@/views/payment/split.vue"),
        name: "PaymentSplit",
        meta: { title: "分账记录" },
      },
      {
        path: "split/gate",
        component: () => import("@/views/payment/split-gate.vue"),
        name: "PaymentSplitGate",
        meta: { title: "开通分账", hidden: true },
      },
      {
        path: "split/bind",
        component: () => import("@/views/payment/split-bind.vue"),
        name: "PaymentSplitBind",
        meta: { title: "绑定分账方", hidden: true },
      },
      {
        path: "split/account",
        component: () => import("@/views/payment/split-account.vue"),
        name: "PaymentSplitAccount",
        meta: { title: "分账开户审核" },
      },
    ],
  },
  {
    path: "/hfpay",
    component: Layout,
    redirect: "/hfpay/index",
    name: "HFPay",
    meta: { title: "汇付支付", icon: "el-icon-sell" },
    children: [
      {
        path: "index",
        component: () => import("@/views/hfpay/index.vue"),
        name: "HFPayIndex",
        meta: { title: "个人开户" },
      },
      {
        path: "detail",
        component: () => import("@/views/hfpay/detail.vue"),
        name: "HFPayDetail",
        meta: { title: "个人开户详情", hidden: true },
      },
      {
        path: "log",
        component: () => import("@/views/hfpay/log.vue"),
        name: "HFPayLog",
        meta: { title: "提现记录" },
      },
    ],
  },
  {
    path: "/external",
    component: Layout,
    redirect: "/external/index",
    name: "External",
    meta: { title: "第三方对接", icon: "el-icon-sell" },
    children: [
      {
        path: "index",
        component: () => import("@/views/external/index.vue"),
        name: "ExternalIndex",
        meta: { title: "第三方管理" },
      },
    ],
  },

  // 外部链接
  // {
  //   path: "/external-link",
  //   component: Layout,
  //   children: [ {
  //       component: () => import("@/views/external-link/index.vue"),
  //       path: "https://www.cnblogs.com/haoxianrui/",
  //       meta: { title: "外部链接", icon: "link" },
  //     },
  //   ],
  // },
  // 多级嵌套路由
  /* {
         path: '/nested',
         component: Layout,
         redirect: '/nested/level1/level2',
         name: 'Nested',
         meta: {title: '多级菜单', icon: 'nested'},
         children: [
             {
                 path: 'level1',
                 component: () => import('@/views/nested/level1/index.vue'),
                 name: 'Level1',
                 meta: {title: '菜单一级'},
                 redirect: '/nested/level1/level2',
                 children: [
                     {
                         path: 'level2',
                         component: () => import('@/views/nested/level1/level2/index.vue'),
                         name: 'Level2',
                         meta: {title: '菜单二级'},
                         redirect: '/nested/level1/level2/level3',
                         children: [
                             {
                                 path: 'level3-1',
                                 component: () => import('@/views/nested/level1/level2/level3/index1.vue'),
                                 name: 'Level3-1',
                                 meta: {title: '菜单三级-1'}
                             },
                             {
                                 path: 'level3-2',
                                 component: () => import('@/views/nested/level1/level2/level3/index2.vue'),
                                 name: 'Level3-2',
                                 meta: {title: '菜单三级-2'}
                             }
                         ]
                     }
                 ]
             },
         ]
     }*/
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

// 全局注册 router
export function setupRouter(app: App<Element>) {
  app.use(router);
}

/**
 * 重置路由
 */
export function resetRouter() {
  router.replace({ path: "/login" });
}

export default router;
